package com.cjhhv.visual.service.impl;

import com.cjhhv.visual.dao.DashboardCategoryDao;
import com.cjhhv.visual.dao.DashboardLogDao;
import com.cjhhv.visual.dao.DashboardThemeDao;
import com.cjhhv.visual.model.DashboardCategoryModel;
import com.cjhhv.visual.model.DashboardLogModel;
import com.cjhhv.visual.model.DashboardThemeModel;
import com.cjhhv.visual.service.DashboardLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional( rollbackFor = Exception.class)
public class DashboardLogServiceImpl implements DashboardLogService {
    private DashboardLogDao dashboardLogDao;
    private DashboardThemeDao dashboardThemeDao;

    private DashboardCategoryDao dashboardCategoryDao;

    @Value("${dashboard.log-save-count}")
    private int logSaveCount;

    public int saveDashboardLog(DashboardLogModel parameter){
       List<String>  idsList = this.dashboardLogDao.queryDashboardLogIdListByParameter(parameter);
       if(null!=idsList && idsList.size()<logSaveCount){
          parameter.setDashboardLogId(UUID.randomUUID().toString());
          return this.dashboardLogDao.saveDashboardLog(parameter);
       }else{
          parameter.setDashboardLogId(idsList.get(idsList.size()-1));
          return this.dashboardLogDao.updateDashboardLogById(parameter);
       }
    }

    public int updateDashboardLogById(DashboardLogModel parameter){
        return this.dashboardLogDao.updateDashboardLogById(parameter);
    }


    public List<String> queryDashboardLogIdListByParameter(DashboardLogModel parameter){
        return this.dashboardLogDao.queryDashboardLogIdListByParameter(parameter);
    }

    public List<DashboardLogModel> queryDashboardLogListByParameter(DashboardLogModel parameter){
        List<DashboardLogModel> list= this.dashboardLogDao.queryDashboardLogListByParameter(parameter);
        if(null!=list && list.size()>0){
            list.forEach(item->{
                if(null!=item.getThemeId() && !"".equals(item.getThemeId())){
                    DashboardThemeModel dashboardThemeModel = this.dashboardThemeDao.queryDashboardThemeById(item.getThemeId());
                    if(null!=dashboardThemeModel){
                        item.setDashboardTheme(dashboardThemeModel);
                    }
                }

                if(null!=item.getDashboardCategoryId() && !"".equals(item.getDashboardCategoryId())){
                    DashboardCategoryModel dashboardCategoryModel = this.dashboardCategoryDao.queryDashboardCategoryById(item.getDashboardCategoryId());
                    if(null!=dashboardCategoryModel){
                        item.setDashboardCategory(dashboardCategoryModel);
                    }
                }

            });
        }

        return list;
    }

    public DashboardLogDao getDashboardLogDao() {
        return dashboardLogDao;
    }

    @Autowired
    public void setDashboardLogDao(DashboardLogDao dashboardLogDao) {
        this.dashboardLogDao = dashboardLogDao;
    }

    public DashboardThemeDao getDashboardThemeDao() {
        return dashboardThemeDao;
    }

    @Autowired
    public void setDashboardThemeDao(DashboardThemeDao dashboardThemeDao) {
        this.dashboardThemeDao = dashboardThemeDao;
    }

    public DashboardCategoryDao getDashboardCategoryDao() {
        return dashboardCategoryDao;
    }

    @Autowired
    public void setDashboardCategoryDao(DashboardCategoryDao dashboardCategoryDao) {
        this.dashboardCategoryDao = dashboardCategoryDao;
    }
}
